<template>
	<view>
		<view class="header">
			<view class="status-bar"></view>
			<view class="nav-bar">
				<u-icon name="arrow-left" color="#FFFFFF" size="48" @click="back"></u-icon>
				<u-search placeholder="搜索商品" v-model="keyword" @click='searchFn' disabled></u-search>
			</view>
		</view>
		<view class="tabBox">
			<view class="tabs">
				<u-tabs :list="tabList" :current="current" @change="tabChange" height='76' bg-color='#F93739'
					inactive-color='#FFFFFF' active-color='#FFFFFF' :show-bar='false'></u-tabs>
			</view>
			<view class="typeBox" @click="showFilterFn">
				<u-icon name="grid" color="#FFFFFF" size="28"></u-icon>
				<text>分类</text>
			</view>
		</view>
		<view class="filterBox" v-show="showFilter">
			<view class="filter-li" :class="{acti:current == index}" v-for="(type,index) in tabList" :key='index'
				@click="choose(index)">
				{{type.name}}
			</view>
		</view>
		<view class="bgc"></view>
		<view class="banner">
			<u-swiper :list="imgs" height='280'></u-swiper>
		</view>
		<view class="typeTab">
			<view class="typeTab-li" :class="{active:tabCurrent==1}" @click="tabChoose(1)">
				<view class="name">
					精选
				</view>
				<view class="tip">
					为你推荐
				</view>
				<view class="icon" v-if="tabCurrent==1">
					<image src="../../../static/imgs/activeicon.png" mode=""></image>
				</view>
			</view>
			<view class="typeTab-li" :class="{active:tabCurrent==2}" @click="tabChoose(2)">
				<view class="name">
					品质
				</view>
				<view class="tip">
					好物热卖
				</view>
				<view class="icon" v-if="tabCurrent==2">
					<image src="../../../static/imgs/activeicon.png" mode=""></image>
				</view>
			</view>
			<view class="typeTab-li" :class="{active:tabCurrent==3}" @click="tabChoose(3)">
				<view class="name">
					实惠
				</view>
				<view class="tip">
					便宜好货
				</view>
				<view class="icon" v-if="tabCurrent==3">
					<image src="../../../static/imgs/activeicon.png" mode=""></image>
				</view>
			</view>
		</view>
		<!-- 京东商品 -->
		<view class="content">
			<u-waterfall v-model="goodsList" ref='goodsWaterfall'>
				<template v-slot:left="{leftList}">
					<view class="goods" v-for="(item, index) in leftList" :key="index"
						@click="navTo('./goods/goods?id='+item.goods_sku_id)">
						<image :src="item.image_path" mode=""></image>
						<view class="title">
							{{item.name}}
						</view>
						<view class="price">
							<view class="tit">
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_price}}
							</view>
							<!-- <view class="tit" v-else>
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_vip_price}}
							</view> -->
							<text class="fukuan">{{item.sales_sum}}人付款</text>
						</view>
						<view class="price-box">
							<view class="rel-price">
								¥<text>{{(Number(item.i_price)-Number(item.use_welfare_fund)).toFixed(2)}}</text>
							</view>
							<!-- 	<view class="rel-price" v-else>
								¥<text>{{(Number(item.i_vip_price)-Number(item.use_welfare_fund)).toFixed(2)}}</text>
							</view> -->
							<view class="fl-price">
								+ {{item.use_welfare_fund}} 福利金
							</view>
						</view>
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="goods" v-for="(item, index) in rightList" :key="index"
						@click="navTo('./goods/goods?id='+item.goods_sku_id)">
						<image :src="item.image_path" mode=""></image>
						<view class="title">
							{{item.name}}
						</view>
						<view class="price">
							<view class="tit">
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_price}}
							</view>
							<!-- 	<view class="tit" v-else>
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_vip_price}}
							</view> -->
							<text class="fukuan">{{item.sales_sum}}人付款</text>
						</view>
						<view class="price-box">
							<view class="rel-price">
								¥<text>{{(Number(item.i_price)-Number(item.use_welfare_fund)).toFixed(2)}}</text>
							</view>
							<!-- 	<view class="rel-price" v-else>
								¥<text>{{(Number(item.i_vip_price)-Number(item.use_welfare_fund)).toFixed(2)}}</text>
							</view> -->
							<view class="fl-price">
								+ {{item.use_welfare_fund}} 福利金
							</view>
						</view>
					</view>
				</template>
			</u-waterfall>
			<view style="padding: 10rpx 0;">
				<u-loadmore bg-color="#f2f2f2" :status="loadStatus"></u-loadmore>
			</view>
		</view>
		<u-mask :show="showMask" @click="close" z-index="97"></u-mask>
		<u-back-top :scroll-top="scrollTop"></u-back-top>
	</view>
</template>

<script>
	import myRequest from '@/api/index.js'
	export default {
		data() {
			return {
				scrollTop: 0,
				isLoading: true,
				showFilter: false,
				showMask: false,
				keyword: '',
				imgs:[],
				tabCurrent: 1,
				current: 0,
				tabList: [],
				page: '1',
				goodsList: [],
				loadStatus: 'loading',
				typeId: '',
				recomId: 1
			}
		},
		onLoad() {
			this.getType()
			this.getBanners()
		},
		onReachBottom() {
			if (!this.isLoading) return;
			let page = Number(this.page) + 1
			this.page = page.toString()
			this.loadStatus = 'loading';
			this.getGoods()
			setTimeout(() => {
				if (!this.isLoading) {
					this.loadStatus = 'nomore'
					this.isLoading = true
				} else {
					this.loadStatus = 'loading';
				}
			}, 1000)
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		methods: {
			async getBanners() {
				let res = await myRequest.getBanner({
					position: '3'
				})
				if (res.status) {
					res.data.forEach(item => {
						this.imgs.push(item.pic_url)
					})
				}
			},
			// 搜索
			searchFn() {
				uni.navigateTo({
					url: './search/search'
				})
			},
			// 京东分类
			async getType() {
				let res = await myRequest.getJdtype()
				if (res.status) {
					// res.data.forEach(item=>{
					// 	item.
					// })
					this.tabList = res.data
					this.typeId = res.data[0].jd_cat
					this.getGoods()
				}
			},
			// 京东商品
			async getGoods() {
				let {
					page,
					typeId,
					recomId,
				} = this
				let res = await myRequest.getGoodsList({
					page: page,
					goods_source: '1',
					cat_id: typeId,
					recommend: recomId.toString(),
				})
				if (res.status) {
					if (res.data.length == 0) {
						this.loadStatus = 'nomore'
						this.isLoading = false
						return
					} else {
						this.goodsList = [...this.goodsList, ...res.data]
					}
				}
			},

			close() {
				this.showFilter = false
				this.showMask = false
			},
			showFilterFn() {
				this.showFilter = !this.showFilter
				this.showMask = !this.showMask
			},
			tabChoose(index) {
				this.$refs.goodsWaterfall.clear()
				this.page = '1'
				this.tabCurrent = index
				this.recomId = index
				this.getGoods()
			},
			tabChange(index) {
				this.current = index
				this.page = '1'
				this.$refs.goodsWaterfall.clear()
				this.typeId = this.tabList[index].jd_cat
				this.getGoods()
			},
			choose(index) {
				this.current = index
				this.showFilter = false
				this.showMask = false
				this.page = '1'
				this.$refs.goodsWaterfall.clear()
				this.typeId = this.tabList[index].jd_cat
				this.getGoods()
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			back() {
				uni.navigateBack({
					delta: 1
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.header {
		width: 100%;
		background-color: #F93739;
		position: sticky;
		top: 0;
		z-index: 99;

		.status-bar {
			width: 100%;
			height: var(--status-bar-height);
		}

		.nav-bar {
			width: 100%;
			height: 88rpx;
			display: flex;
			align-items: center;
			padding: 0 20rpx 0 10rpx;

			.logo {
				width: 68rpx;
				height: 68rpx;
				margin-right: 12rpx;
			}
		}
	}

	.tabBox {
		width: 100%;
		height: 84rpx;
		background-color: #F93739;
		display: flex;
		align-items: center;
		position: sticky;
		// #ifdef APP-PLUS
		top: 128rpx;
		// #endif
		z-index: 98;

		.tabs {
			width: 80%;
		}

		.typeBox {
			width: 20%;
			height: 84rpx;
			background-color: #F93739;
			display: flex;
			align-items: center;
			justify-content: center;
			line-height: 84rpx;

			text {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
				margin-left: 6rpx;
			}
		}
	}

	.filterBox {
		width: 100%;
		height: auto;
		background-color: #FFFFFF;
		display: flex;
		flex-wrap: wrap;
		position: sticky;
		// #ifdef APP-PLUS
		top: 212rpx;
		// #endif
		z-index: 98;
		padding: 20rpx 20rpx 0;

		.acti {
			border: 2rpx solid #F93739;
		}

		.filter-li {
			width: 160rpx;
			height: 66rpx;
			background: #F8F8F8;
			border-radius: 4rpx;
			margin-right: 22rpx;
			text-align: center;
			line-height: 66rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #666666;
			margin-bottom: 20rpx;

			&:nth-child(4n) {
				margin-right: 0rpx;
			}
		}
	}

	.bgc {
		width: 100%;
		height: 160rpx;
		background-color: #F93739;
	}

	.banner {
		padding: 0 20rpx;
		margin-top: -150rpx;
	}

	.typeTab {
		width: 100%;
		height: 104rpx;
		display: flex;
		align-items: center;
		margin: 24rpx 0;

		.typeTab-li {
			flex: 1;
			height: 100%;
			display: flex;
			flex-direction: column;

			.name,
			.tip {
				width: 100%;
				text-align: center;
			}

			.name {
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #222222;
			}

			.tip {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #999999;
			}

			.icon {
				margin: 0 auto;
				width: 32rpx;
				height: 14rpx;

				image {
					width: 32rpx;
					height: 14rpx;
				}
			}
		}

		.active {
			.name {
				font-weight: 600;
			}

			.tip {
				color: #F93739;
			}
		}
	}

	.content {
		padding: 0 15rpx;

		.goods {
			width: 348rpx;
			height: auto;
			background-color: #FFFFFF;
			border-radius: 16rpx;
			padding-bottom: 18rpx;
			overflow: hidden;
			margin-top: 20rpx;

			image {
				width: 348rpx;
				height: 348rpx;
			}

			.price-box {
				padding-left: 10rpx;
				width: fit-content;
				width: -webkit-fit-content;
				width: -moz-fit-content;
				display: flex;
				margin-top: 12rpx;

				.rel-price {
					height: 44rpx;
					width: fit-content;
					width: -webkit-fit-content;
					width: -moz-fit-content;
					background: linear-gradient(180deg, #FBE9B9 0%, #FDE6B8 100%);
					border-radius: 8rpx 0px 0px 8rpx;
					padding: 0 6rpx;
					font-size: 24rpx;
					font-family: Arial;
					font-weight: 400;
					line-height: 44rpx;
					color: #333333;

					text {
						font-size: 32rpx;
						font-weight: bold;
						margin-left: 4rpx;
					}
				}

				.fl-price {
					height: 44rpx;
					width: fit-content;
					width: -webkit-fit-content;
					width: -moz-fit-content;
					background: #333333;
					border-radius: 0px 8rpx 8rpx 0px;
					font-size: 24rpx;
					font-family: Arial;
					font-weight: 400;
					color: #FBE8B8;
					line-height: 44rpx;
					padding: 0 6rpx;
				}
			}

			.title {
				margin-top: 6rpx;
				padding-left: 12rpx;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #333333;
			}

			.price {
				margin-top: 6rpx;
				padding-left: 12rpx;
				padding-right: 12rpx;
				display: flex;
				align-items: center;

				.tit {
					font-size: 36rpx;
					font-family: Arial;
					font-weight: 600;
					color: #FF4600;
					text-decoration: line-through
				}

				.fukuan {
					margin-left: auto;
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #999999;
				}
			}
		}
	}

	/deep/ .u-action-active {
		display: none;
	}
</style>
